package com.jxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxx.mapper.AddressMapper;
import com.jxx.mapper.UserInfoMapper;
import com.jxx.mapper.UserMapper;
import com.jxx.mapper.UserSecurityMapper;
import com.jxx.pojo.Address;
import com.jxx.pojo.Password;
import com.jxx.pojo.User;
import com.jxx.pojo.UserInfo;
import com.jxx.service.UserService;
import com.jxx.vo.SysResult;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserSecurityMapper userSecurityMapper;
    @Autowired
    private AddressMapper addressMapper;

    private static Map<Integer,String> paramMap = new HashMap<>();
    static {
        paramMap.put(1, "user_account");
        paramMap.put(2, "phone");//备用字段
        paramMap.put(3, "email");//备用字段
    }

    /**
     * @author LKS
     * @desc 门户端用户注册
     * @param userAccount
     * @param userPassword
     * @param cfgPassword
     * @return SysResult
     * @data 2020年11月18日23:19:56
     */
    @Override
    public SysResult register(String userAccount, String userPassword, String cfgPassword) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", userAccount);
        int count = userMapper.selectCount(queryWrapper);
        if (count>0) {
            return SysResult.fail("该用户已存在");
        }else {
            User user = new User();
            user.setUserAccount(userAccount);
            user.setUserPassword(userPassword);
            String salt = UUID.randomUUID().toString();
            SimpleHash sh = new SimpleHash(
                    "MD5",
                    userPassword,
                    salt,
                    1);
            user.setSalt(salt);
            user.setUserPassword(sh.toHex());
            userMapper.insert(user);
            return SysResult.success();
        }
    }

    /**
     * @author LKS
     * @desc 门户端用户注册
     * Sql: select count(*) from user where userAccount="admin123";
     * @param userAccount
     * @return boolean
     * @data 2020年11月18日23:19:56
     */
    @Override
    public boolean checkUser(String userAccount,Integer type) {
        String column = paramMap.get(type);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column,userAccount);
        int count = userMapper.selectCount(queryWrapper);
        return count>0?true:false;
    }
    /**
    * @Description: 增加用户收货地址
    * @Param: [address]
    * @return: int
    * @Author: laixinghua
    * @Date: 2020/11/23 0023 下午 7:44
    */
    @Override
    public int insertaddress(Address address) {
        int i=addressMapper.insert(address);
        return i;
    }
    /**
    * @Description: 根据用户id查找地址
    * @Param: [userId]
    * @return: java.util.List<com.jxx.pojo.Address>
    * @Author: laixinghua
    * @Date: 2020/11/24 0024 上午 8:57
    */
    @Override
    public List<Address> findaddressByUserId(Integer userId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List addresses = addressMapper.selectList(queryWrapper);
        return addresses;
    }
    /**
     * @Description: 根据地址id删除地址
     * @Param: [addressId]
     * @return: int
     * @Author: laixinghua
     * @Date: 2020/11/24 0024 下午 8:27
     */
    @Override
    public int deleteAddressByaddressId(Integer addressId) {
        int i = addressMapper.deleteById(addressId);
        return i;
    }
    /**
     * @Description: 根据地址id查找收货地址
     * @Param: []
     * @return:
     * @Author: laixinghua
     * @Date: 2020/11/26 0026 下午 1:25
     */
    @Override
    public Address findaddressByaddressId(Integer addressId) {
        Address address = addressMapper.selectById(addressId);
        return address;
    }
    /**
     * @Description: 根据地址id更改收货地址
     * @Param: [addressId]
     * @return: int
     * @Author: laixinghua
     * @Date: 2020/11/26 0026 下午 6:45
     */

    @Override
    public int updateAddressByaddressId(Address address) {
        int i = addressMapper.updateById(address);
        return i;
    }
    /**
     * @Description: 根据用户id查找密码
     * @Param: [userId]
     * @return: java.lang.String
     * @Author: laixinghua
     * @Date: 2020/11/28 0028 上午 10:30
     */
    @Override
    public String findPasswordByUserId(Integer userId) {
        User user = userMapper.selectById(userId);
        String userPassword=user.getUserPassword();
        return userPassword;
    }
    /**
     * @Description: 更改用户密码
     * @Param: [password]
     * @return: int
     * @Author: laixinghua
     * @Date: 2020/11/28 0028 上午 10:31
     */
    @Override
    public int updatePassword(String newPassword,Integer userId) {

        int i = userMapper.updateById(new User().setUserId(userId).setUserPassword(newPassword));
        return i;
    }
    /**
     * @Description: 根据用户id查用户信息
     * @Param: [userId]
     * @return: com.jxx.pojo.UserInfo
     * @Author: laixinghua
     * @Date: 2020/12/7 0007 下午 7:47
     */
    @Override
    public UserInfo findUserInfoByUserId(Integer userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return userInfo;
    }
    /**
     * @Description: 根据用户Id增加用户信息
     * @Param: [userInfo]
     * @return: int
     * @Author: laixinghua
     * @Date: 2020/12/8 0008 上午 9:45
     */
    @Override
    public int insertUserInfo(UserInfo userInfo) {
        int insert = userInfoMapper.insert(userInfo);
        return insert;
    }
    /**
     * @Description: 根据用户Id增加用户信息
     * @Param: [userInfo]
     * @return: int
     * @Author: laixinghua
     * @Date: 2020/12/8 0008 上午 9:45
     */
    @Override
    public int UpdateUserInfo(UserInfo userInfo) {
        int i = userInfoMapper.updateById(userInfo);
        return i;
    }

}
